<template>
  <div class="bill-edit">
    <header class="bill-edit-header">
      <BillEditHeader :billType="billType" @setBillType="setBillType" />
    </header>
    <main class="bill-edit-main">
      <div class="bill-edit-value">
        <div class="current-tag">
          <div 
            :style="`background-color:${getCurrentTag.color}`" 
            class="tag-icon">
              <van-icon :name="getCurrentTag.icon" size="6.25vw"/>
          </div>
          <span class="current-tag-name">
            {{ getCurrentTag.name }}
          </span>
        </div>
        <div class="current-value" :title="getCurrentValue">
          {{ getCurrentValue }}
        </div>
      </div>
      <div class="bill-edit-tags">
        <div 
          v-for="item in getTagList"
          :key="item.tagId"
          class="bill-edit-tag">
          <div 
            @click="setEditTagId(item.tagId)"
            :style="`background-color:${item.color}`" 
            class="tag-icon">
            <van-icon 
              :name="item.icon" 
              size="6.25vw"/>
          </div>
          <span class="tag-name">
            {{ item.name }}
          </span>
        </div>
        <div class="bill-edit-tag">
          <div 
            @click="addBillTag"
            :style="`background-color:#ADADAD`" 
            class="tag-icon">
              <van-icon name="plus" size="6.25vw"/>
          </div>
          <span class="tag-name">
            添加
          </span>
        </div>
      </div>
    </main>
    <footer class="bill-edit-footer">
      <BillEditFooter 
        :tagId="tagId"
        :billType="billType"
        @updateCurrentValue="updateCurrentValue"/>
    </footer>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { BillModule, BillType, IBillData } from '../../../store/modules/bill';
import { UserModule, IBillTag } from '../../../store/modules/user';
import { formatNumber } from './../../../utils/formatNumber';
import BillEditHeader from './BillEditHeader.vue';
import BillEditFooter from './BillEditFooter.vue';

@Component({
  name: 'BillEdit',
  components: {
    BillEditFooter,
    BillEditHeader,
  },
})
export default class extends Vue {
  private currentUserConfig = UserModule.userConfig;
  // 判断是否编辑状态，编辑状态即给目标赋值
  private billType: BillType = BillType.Expense;
  private tagId: number = this.currentUserConfig.billTagList[0].tagId;
  private currentValue: string = '0';

  public mounted() {
    if (BillModule.isEidtBillStatus) {
      this.billType = this.getCurrentBill().billType;
      this.tagId = this.getCurrentBill().billTagId;
      this.currentValue = this.getCurrentBill().amount.toString();
    }
    if (!BillModule.isEidtBillStatus && !BillModule.isAddBillStatus) {
      const router = '$router';
      this[router].push({ path: 'bill' });
    }
  }

  private get getCurrentTag() {
    return this.getTagList.find((item: IBillTag) => {
      return item.tagId === this.tagId;
    });
  }
  private get getCurrentValue(): string {
    return formatNumber(this.currentValue);
  }
  private get getTagList(): IBillTag[] {
    return this.currentUserConfig.billTagList.filter((item) => {
      return item.type === this.billType;
    });
  }

  public setBillType(newBillType: BillType) {
    this.billType = newBillType;
    this.tagId = this.getTagList[0].tagId;
  }
  public updateCurrentValue(newValue: string) {
    this.currentValue = newValue;
  }
  private setEditTagId(id: number) {
    this.tagId = id;
  }
  private getCurrentBill(): IBillData {
    return BillModule.bills.find((item) => {
      return item.billId === BillModule.currentEditBillId;
    }) as IBillData;
  }
  private addBillTag() {
    const router = '$router';
    this[router].push({ path: 'bill-tag-add' });
    UserModule.toggleIsAddBillTagStatus(true);
  }

}
</script>

<style scoped>
.bill-edit {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: auto;
}
.bill-edit-header {
  height: 80px;
}
.bill-edit-main {
  flex: 1;
  height: 100%;
  overflow: auto;
}
.bill-edit-value {
  display: flex;
  align-items: center;
  height: 100px;
  border-bottom: 1px solid #D0CECF;
}
.current-tag {
  display: flex;
  align-items: center;
  height: 100px;
  width: 300px;
  padding-left: 30px;
}
.current-tag-name {
  float: right;
  flex: 1;
  height: 100px;
  line-height: 100px;
  padding-left: 20px;
  font-size: 40px;
  overflow: hidden;
}
.current-value {
  flex: 1;
  width: 100%;
  height: 100px;
  font-size: 50px;
  font-weight: bold;
  line-height: 100px;
  text-align: right;
  padding-right: 20px;
  overflow: hidden;
  text-overflow:ellipsis;
}
.bill-edit-tags {
  overflow: scroll;
  padding-top: 20px;
}
.bill-edit-tag {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  float: left;
  width: 20%;
  height: 150px;
}
.tag-icon {
  width: 75px;
  height: 75px;
  border-radius: 50%;
  color: white;
  text-align: center;
}
.tag-icon .van-icon{
  line-height: 75px;
}
.tag-name {
  height: 50px;
  line-height: 50px;
  font-size: 25px;
  overflow: hidden;
}
</style>